package com.zzj.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zzj.annotation.LogAnnotation;
import com.zzj.base.Result;
import com.zzj.entity.Tag;
import com.zzj.model.dto.QueryListDto;
import com.zzj.model.dto.TagsCreateDto;
import com.zzj.service.ITagService;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 标签 前端控制器
 * </p>
 *
 * @author zzj
 * @since 2023-07-17
 */
@RestController
@RequestMapping("/tag")
public class TagController {
    @Resource
    private ITagService tagService;

    @LogAnnotation(name = "标签创建",description = "传入标签名创建标签")
    @Transactional
    @PostMapping("create")
    public Result create(@RequestBody @Validated TagsCreateDto in) {
        Tag tag = tagService.findTag(in.getName());
        if(tag!=null){
            return  Result.error("标签已存在");
        }
        Tag create = tagService.create(in);
        return Result.success(create);
    }

    @LogAnnotation(name = "查询标签",description = "分页查询标签列表")
    @Transactional
    @PostMapping("queryList")
    public Result queryList(@RequestBody @Validated QueryListDto in) {
        IPage<Tag> tagIPage = tagService.queryList(in);
        return Result.success(tagIPage);
    }

    @LogAnnotation(name = "删除标签",description = "根据ID删除标签")
    @Transactional
    @PostMapping("remove")
    public Result remove(@RequestParam(value = "id") int id) {
        boolean b = tagService.removeById(id);
        if (!b) {
            return Result.error("删除失败");
        }
        return Result.success("删除成功");
    }

}
